package com.xingcheng.discord.midjourney;

import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.utils.data.DataObject;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author xingchen
 * @version 1.0.0
 * @title ImageEventHandler
 * @description
 * @create 2023/12/31 16:11
 **/

@Service
@Slf4j
public class ImageEventHandler implements EventHandle{
    @Override
    public void handle(MessageType messageType, DataObject data) {
        log.info("ImageEventHandler handle:{}",data.toString());

        if (MessageType.CREATE.equals(messageType)&& hasImage(data)) {
          //todo  生成图片之后的操作
        }else if (MessageType.INTERACTION_SUCCESS.equals(messageType)){
            //todo  remix开启时获取交互Id
            //获取messageId 可以填入modal 结构中的dataId
            String messageId = data.getString("id");
            String nonce=null;
            try {
                nonce = data.getString("nonce");
            }catch (Exception e){
                log.error("获取messageId失败",e);
                return;
            }

        }else if(MessageType.INTERACTION_IFRAME_MODAL_CREATE.equals(messageType)){
            log.info("varyRegion INTERACTION交互回调:{}", data.toString());
            // varyRegion 获取custom_id 注,custom_id 需要在socket 连接成功时把sessionId记录一下
            String customId = data.getString("custom_id");
            if(!ObjectUtils.isEmpty(customId)){
                customId = customId.split("::")[2];
            }
            String nonce=null;
            try {
                nonce = data.getString("nonce");
                log.info("获取nonce随机数成功:{}",nonce);
            }catch (Exception e){
                log.error("获取nonce随机数失败",e);
                return;
            }
            //customId 在局部重绘时填充进去
        }

    }
}
